/*
 * 程序入口模块
 *
 * 负责启动所有组件，设置守护进程模式
 *
 **/

#include <unistd.h>
#include "config.hh"
#include "business.hh"
#include "server.hh"

namespace atm_server
{

class entry
{
public:
    entry()
        : _svr(server::instance(config::instance().server_port()))
    {}

    static void daemonize()
    {
        int id = fork();

        if (id < 0)
            easylog::fatal("daemonize fork failed");
        else if (id > 0)
            exit(0);

        if (setsid() < 0)
            easylog::fatal("daemonize setsid failed");

        umask(00);
    }

    entry& regist()
    {
        _svr.regist(business::balance, business::deposit, business::withdraw, business::transfer);
        return *this;
    }

    void run()
    {
        _svr.run();
    }

private:
    server& _svr;
};

}

int main()
{

    easylog::info("server process started");

    atm_server::entry().regist().run();

    return 0;
}
